package com.lt.bcra.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lt.bcra.mapper.BcraPhysiologicalFactorsMapper;
import com.lt.bcra.entity.BcraPhysiologicalFactors;
import com.lt.bcra.exception.BaseException;
import com.lt.bcra.service.BcraPhysiologicalFactorsService;
import com.lt.bcra.result.ResponseEnum;
import com.lt.bcra.result.ResultData;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 生理因素表 服务实现类
 * </p>
 *
 * @author LuoTian
 * @since 2022-05-25
 */
@Service
public class BcraPhysiologicalFactorsServiceImpl extends ServiceImpl<BcraPhysiologicalFactorsMapper, BcraPhysiologicalFactors> implements BcraPhysiologicalFactorsService {

    @Autowired
    BcraPhysiologicalFactorsMapper bcraPhysiologicalFactorsMapper;

    @Override
    public ResultData saveModule(BcraPhysiologicalFactors bcraPhysiologicalFactors) {

        bcraPhysiologicalFactors.setModuleTitle("生理因素表");
        bcraPhysiologicalFactors.setModuleId(2);

        int ok = bcraPhysiologicalFactorsMapper.insert(bcraPhysiologicalFactors);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.CREATE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData removeByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraPhysiologicalFactors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        int ok = bcraPhysiologicalFactorsMapper.delete(queryWrapper);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.DELETE_DATA_ERROR);
        }
        return ResultData.getResult(ok);

    }

    @Override
    public ResultData updateModule(BcraPhysiologicalFactors bcraPhysiologicalFactors) {
        QueryWrapper<BcraPhysiologicalFactors> queryWrapper =new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code",bcraPhysiologicalFactors.getQuestionnaireCode());
        int ok = bcraPhysiologicalFactorsMapper.update(bcraPhysiologicalFactors, queryWrapper);
        if (ok != 1) {
            throw new BaseException(ResponseEnum.UPDATE_DATA_ERROR);
        }
        return ResultData.getResult(ok);
    }

    @Override
    public ResultData selectByQuesCode(String questionnaireCode) {
        QueryWrapper<BcraPhysiologicalFactors> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("questionnaire_code", questionnaireCode);
        BcraPhysiologicalFactors bcraPhysiologicalFactors = bcraPhysiologicalFactorsMapper.selectOne(queryWrapper);
        if (bcraPhysiologicalFactors == null) {
            throw new BaseException(ResponseEnum.SELECT_DATA_ERROR);
        }
        return ResultData.getResult(bcraPhysiologicalFactors);
    }
}
